import Environment from "./environment";

const Log = (function() {
    function Log() {}

    function Record(level, message) {
        this.level = level;
        this.time = new Date().formatToLog();
        this.message = message;
    }

    let Levels = {
        Debug: {
            name: "调试",
            type: "info",
            scale: 0
        },
        Info: {
            name: "信息",
            type: "success",
            scale: 1
        },
        Warning: {
            name: "警告",
            type: "warning",
            scale: 2
        },
        Error: {
            name: "错误",
            type: "error",
            scale: 3
        }
    };

    const level = Environment.isDevelopment() ? Levels.Debug : Levels.Error;

    Log.debug = function(message) {
        if (level.scale <= Levels.Debug.scale) {
            Log._log2Console(new Record(Levels.Debug, message));
        }
    };

    Log.info = function(message) {
        if (level.scale <= Levels.Debug.scale) {
            Log._log2Console(new Record(Levels.Info, message));
        }
    };

    Log.warn = function(message) {
        if (level.scale <= Levels.Debug.scale) {
            Log._log2Console(new Record(Levels.Warning, message));
        }
    };

    Log.error = function(message) {
        if (level.scale <= Levels.Debug.scale) {
            Log._log2Console(new Record(Levels.Error, message));
        }
    };

    Log._log2Console = function(record) {
        console.log(record.time + " 【" + record.level.name + "】 " + record.message);
    };

    return Log;
})();

export default Log;
